package elements 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import flash.utils.Dictionary;
	
	/**
	 * 
	 * @author kasoi
	 */
	public class VersionElement extends Sprite 
	{
		
		////////////////////////////////////////////////////////////////////
		// Public properties
		////////////////////////////////////////////////////////////////////
		
		////////////////////////////////////////////////////////////////////
		// Private properties
		////////////////////////////////////////////////////////////////////
		
		private var field: TextField;
		
		private var version: String;
		
		private var key: String;
		
		private var shift: Boolean;
		
		private var control: Boolean;
		
		////////////////////////////////////////////////////////////////////
		// Public methods
		////////////////////////////////////////////////////////////////////
		
		/*
		 * @param version: Сюда вписывается версия в стринге, например "0.1"
		 * @param key: буква на клаве, которую нужно нажать
		 */
		public function VersionElement(version: String, key: String = 'd', 
			shift: Boolean = false, control: Boolean = true) 
		{
			this.version = version;
			this.key = key.toUpperCase();
			this.shift = shift;
			this.control = control;
			this.init();
		}
		
		public function show() : void 
		{
			this.field.visible = true;
		}
		
		public function hide() : void 
		{
			this.field.visible = false;
		}
		
		public function toggle() : void 
		{
			this.field.visible = !this.field.visible;
		}
		
		////////////////////////////////////////////////////////////////////
		// Private methods
		////////////////////////////////////////////////////////////////////
		
		private function init() : void
		{
			this.field = new TextField();
			this.field.defaultTextFormat = new TextFormat('Verdana',
				12, 0xffffff);
			this.field.text = 'v:' + this.version;
			this.field.filters = [new GlowFilter(0, 1, 8, 8, 3, 2)];
			
			this.addChild(this.field);
			
			this.field.x = this.field.y = 20;
			
			this.hide();
			
			this.addEventListener(Event.ADDED_TO_STAGE,
				this.this_onAddedToStage);
		}
		
		////////////////////////////////////////////////////////////////////
		// Listeners
		////////////////////////////////////////////////////////////////////
		
		private function stage_onKeyDown(e: KeyboardEvent) : void 
		{
			var isPressed: Boolean = false;
			var key: Boolean = (e.keyCode == this.key.charCodeAt(0));
			var shift: Boolean = true;
			if (this.shift) 
			{
				shift = (e.shiftKey == this.shift);
			}
			var control: Boolean = true;
			if (this.control)
			{
				control = (e.ctrlKey == this.control);
			}
			
			if (key && shift && control)
			{
				this.toggle();
			}
		}
		
		private function this_onAddedToStage(e: Event) : void 
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN,
				this.stage_onKeyDown);
		}
		
	}
	
}